//获取应用实例
const app = getApp()


//POST、PUT、DELETE、GET请求的基础封装
function showErroMsg(msg) {
  wx.showToast({
    title: msg || '网络异常，请稍候重试',
    icon: 'none',
    mask: true,
  })
}

function cleanLoginInfo(statusCode) {
  if (statusCode == 401) {
    app.globalData.token = ''
    app.cleanGlobalUserInfoCallback = () => {
      app.globalData.userInfo = null
      if (app.userInfoReadyCallback) {
        app.userInfoReadyCallback({ userInfo: null })
      }
    }


    app.cleanGlobalUserInfoCallback()
  }
}



function vertifyToken() {
  return app.globalData.token
}


module.exports = {

  POST: (url, data, code200, error) => {

    let token = vertifyToken()

    wx.request({
      url: url,
      method: "POST",
      header: {
        'authorization': token
      },
      data: data,
      success: res => {
        console.log("success")
        console.log(res.data)
        // console.log(res.statusCode)
        if (res.data.success) {
          if (code200) code200(res.data)
        } else {
          showErroMsg(res.data.message)
        }
      },
      fail: res => {
        console.log("fail")
        console.log(res)
        showErroMsg()
      },
      complete: res => {

      }
    })
  },

  GET: (url, data, code200, error) => {

    let token = vertifyToken()
    wx.request({
      url: url,
      method: "GET",
      header: {
        'authorization': token
      },
      data: data,
      success: res => {
        console.log("success")
        console.log(res.data)
        cleanLoginInfo(res.statusCode)
        if (res.data.success) {
          if (code200) code200(res.data)
        } else {
          if(token){
            showErroMsg(res.data.message)
          }
        }
      },
      fail: res => {
        console.log("fail")
        console.log(res)
        showErroMsg()
      },
      complete: res => {
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },

  PUT: (url, data, code200, error) => {

    let token = vertifyToken()

    wx.request({
      url: url,
      method: "PUT",
      header: {
        'authorization': token
      },
      data: data,
      success: res => {
        console.log("success")
        console.log(res.data)
        if (res.data.success) {
          if (code200) code200(res.data)
        } else {
          showErroMsg(res.data.message)
        }
      },
      fail: res => {
        console.log("fail")
        console.log(res)
        showErroMsg()
      },
      complete: res => {

      }
    })
  },

  DELETE: (url, data, code200, error) => {

    let token = vertifyToken()

    wx.request({
      url: url,
      method: "DELETE",
      header: {
        'authorization': token
      },
      data: data,
      success: res => {
        console.log("success")
        console.log(res.data)
        if (res.data.success) {
          if (code200) code200(res.data)
        } else {
          showErroMsg(res.data.message)
        }
      },
      fail: res => {
        console.log("fail")
        console.log(res)
        showErroMsg()
      },
      complete: res => {

      }
    })
  },

}


const wxGet = function (url, data, code200, error) {
  wx.request({
    url: url,
    data: data,
    success: res => {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.data.msg === '0') {
        code200(res)
      } else {
        if (error) {
          error(res)
        } else {
          // console.log(res)
          console.log('请求出错：msg:' + res.data.msg + '---' + url)
        }
      }
    },
    fail: res => {
      wx.stopPullDownRefresh() //停止下拉刷新
      wx.showToast({
        title: '网络出现问题，请稍候重试',
        icon: 'none',
        mask: true,
      })
      if (error) {
        error(res)
      } else {
        console.log(res)
        console.log('执行fail：' + url)
      }
    }
  })
}
